<!DOCTYPE HTML>
<script src="../../resources/js-test.js"></script>
<script>
description('Tests for writing and reading .type property of HTMLInputElement.');

var input = document.createElement('input');

// The default type is "text".
shouldBe('input.type', '"text"');
shouldBeNull("input.getAttribute('type')");

function check(value, expected, expectedAttributeValue)
{
    input.type = value;
    if (input.type === expected)
        testPassed('input.type for "' + value + '" is correctly "' + input.type + '".');
    else
        testFailed('input.type for "' + value + '" is incorrectly "' + input.type + '", should be "' + expected + '".');

    if (typeof expectedAttributeValue === "undefined")
        expectedAttributeValue = expected;

    if (input.getAttribute('type') === expectedAttributeValue)
        testPassed('input.getAttribute("type") for "' + value + '" is correctly "' + expectedAttributeValue + '".');
    else
        testFailed('input.getAttribute("type") for "' + value + '" is incorrectly "' + input.getAttribute('type') + '", should be "' + expectedAttributeValue + '".');
}

check("text", "text");
check("TEXT", "text", "TEXT");  // input.type must return a lower case value according to DOM Level 2.
check(" text ", "text", " text ");
check("button", "button");
check(" button ", "text", " button ");
check("checkbox", "checkbox");
check("chec\u212Abox", "text", "chec\u212Abox");
check("email", "email");
check("file", "file");
check("hidden", "hidden");
check("image", "image");
check("isindex", "text", "isindex");
check("number", "number");
check("password", "password");
check("passwd", "text", "passwd");
check("radio", "radio");
check("range", "range");
check("reset", "reset");
check("search", "search");
check("submit", "submit");
check("tel", "tel");
check("telephone", "text", "telephone");
check("url", "url");
check("uri", "text", "uri");

// Empty and unknown value handling.
check("", "text", "");
check("x-unknown", "text", "x-unknown");
shouldBeNull("input.removeAttribute('type'); input.getAttribute('type')");

debug("Check dirty flag behavior");
input = document.createElement("input");
input.type = "hidden";
input.defaultValue = "Default";
input.type = "text";
// The dirty flag should be still false, and the defaultValue should be reflected
// to value.
shouldBeEqualToString("input.defaultValue = 'UpdatedDefault'; input.value", "UpdatedDefault");

debug("Changing the type to file should discard the dirty value.");
input = document.createElement("input");
input.setAttribute("value", "DEFAULT");
input.value = "DIRTY";
input.type = "file";
input.type = "text";
shouldBe("input.value", "input.getAttribute('value')");

</script>
